package com.wu.concurencys.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 *
 */
@Slf4j
public class SemaphoreExample1 {

    private  static final int threadcount = 20;
    public static void main(String[] args) throws  Exception{

        ExecutorService exec = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(3);
        for (int i = 0;i<threadcount;i++)
        {
            final  int threadnumber = i;
            exec.execute(() ->{
               try {
                   semaphore.acquire(); //  获取一个许可
                   test(threadnumber);
                   semaphore.release(); // 释放一个许可
               }catch (Exception e)
               {
                   log.info("{}",e);
               }finally {

                }
            });
        }

        log.info("finash*****");
        exec.shutdown();// 关闭线程池

    }

    private static void test(int number) throws  Exception
    {

        log.info("{}",number);
        Thread.sleep(1000);

    }
}
